//生产管理系统/数据趋势分析
import React, { Component } from 'react';
import { connect } from 'dva';
import MyIcon from '../../../components/common/MyIcon';
import {Tag ,Form,Table,Select, Card, DatePicker, Button, message, Pagination } from 'antd';
import style from './Inquire.less';
import {querypaging,repairRefer} from "../../../services/redmoe";
import Examine from "./modal/Examine";
import moment from 'moment';
import publicService from "../../../services/PublicService"
import request from '../../../utils/request';
const { MonthPicker, RangePicker, WeekPicker } = DatePicker;
//下拉选择器联动套件
const Option = Select.Option;
const provinceData = ['日报表', '自定义报表'];
const cityData = {
  日报表: ['能源消耗统计表', '土壤处理量统计表'],
  自定义报表: ['能源消耗统计表', '土壤修复指标汇总表', '土壤处理量统计表'],
};

const rowSelection = {
    onChange: (selectedRowKeys, selectedRows) => {
      console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
    },
    getCheckboxProps: record => ({
      disabled: record.name === 'Disabled User', // Column configuration not to be checked
      name: record.name,
    }),
  };

//下拉联动套件


class Inquire extends Component {
    state = {
        tableLoading:false,
        RangePicker:null,
        repairWidth:null,
        tagVisible:false,
        endDate:"",
        rowCount:null,
        startDate:"",
        reportType:0,
        type:null,
        type1:"日报表",
        over:[],
        cities: cityData[provinceData[0]],
        secondCity: cityData[provinceData[0]][0],
        row1:{},
          row:{},
          btn:"",
          loading: false,
          visible: false,
        record:{},
          page:1,
          rows:10,
          text: "能源消耗统计表",
          data: [],
          columns: [
              {
                  title: "序号",
                  dataIndex: "num",
                  key: "num",
                  align: "center",
                    width:'80'
              },
              {
                  title: "报表类型",
                  dataIndex: "reportType",
                  key: "reportType",
                  align: "center",
                  width:'200'
              },
              {
                  title: "报表名称",
                  dataIndex: "reportChineseName",
                  key: "reportChineseName",
                  align: "center",
                  width:'170'
              },
              {
                  title: "日期",
                  key: "takeTime",
                  dataIndex: "takeTime",
                  align: "center",
                  width:170
              },
              {
                  title: "操作",
                  key: "useElectric",
                  dataIndex: "useElectric",
                  align: "center",
                  width:'200',
                  render:(text,record,index) => <span><Tag color="blue" size="small" onClick={this.showModal.bind(this,record)}>查看</Tag></span>
              }
          ]
      };

  //弹出框套件
  showModal = (record) => {
    console.log(record)
    //日报表查询请求
    if(this.state.text == '能源消耗统计表') {
        request(`/hh-management-web/gnhj/energyconsumption/queryEnergyConsumptionDayList?takeTime=${record.takeTime}`).then(data => {
            console.log(data)
            if(data.data.rc == 0) {
                console.log(data.data.ret)
                this.setState({
                    over:data.data.ret
                   
                })
                console.log(this.state.over)
            }
        })
    }

    if(this.state.text == '土壤处理量统计表') {
        request(`/hh-management-web/gnhj/soilTreatment/querySoilTreatmentDayList?takeTime=${record.takeTime}`).then(data => {
            console.log(data)
            if(data.data.rc == 0) {
                console.log(data.data.ret)
                this.setState({
                    over:data.data.ret
                   
                })
                console.log(this.state.over)
            }
        })
    }



    
    this.setState({ visible: true,
        record:record
    });
  }

  handleCancel = () => {
    this.setState({ visible: false });
  }

  handleCreate = () => {
    const form = this.formRef.props.form;
    form.validateFields((err, values) => {
      if (err) {
        return;
      }

      console.log('Received values of form: ', values);
      form.resetFields();
      this.setState({ visible: false });
    });
  }

  saveFormRef = (formRef) => {
    this.formRef = formRef;
  }

//下拉选择器套件
handleProvinceChange = (value) => {
    console.log(value)
    this.setState({
      cities: cityData[value],
      secondCity: cityData[value][0],
      type:value,
      type1:value
    });
}

  onSecondCityChange = (value) => {
    this.setState({
      secondCity: value,
      text:value
    });
    console.log(value)
  }

  //日期选择器的change事件
  RangePicker = (data,datastring) => {

   console.log(datastring[0])
    this.setState({
        startDate:datastring[0],
        endDate:datastring[1],
        RangePicker:[data[0],data[1]]
    })

 
  }
   //页面进入事件
    componentDidMount() {
        this.setState({
            reportType:0,

        })
        let data = {
            page:1,
            reportName:this.state.secondCity,
            reportType:this.state.reportType,
            rows:10
        }
        querypaging({...data}).then(data => {
            console.log(data)
            let arr = data.data.ret.items
            arr.forEach(item => {
              item.takeTime = moment(item.takeTime).format("YYYY-MM-DD")
              if(item.reportType == 0) {
                  item.reportType="日报表"
              }
            });
            this.setState({
            tagVisible:false,
              data:data.data.ret.items,
              rowCount:data.data.ret.rowCount,
              columns: [
                {
                    title: "序号",
                    dataIndex: "num",
                    key: "num",
                    align: "center"
                },
                {
                    title: "报表类型",
                    dataIndex: "reportType",
                    key: "reportType",
                    align: "center"
                },
                {
                    title: "报表名称",
                    dataIndex: "reportChineseName",
                    key: "reportChineseName",
                    align: "center"
                },
                {
                    title: "日期",
                    key: "takeTime",
                    dataIndex: "takeTime",
                    align: "center"
                },
                {
                    title: "操作",
                    key: "useElectric",
                    dataIndex: "useElectric",
                    align: "center",
                    render:(text,record,index) => <span><Tag color="blue" size="small" onClick={this.showModal.bind(this,record)}>查看</Tag></span>
                }
            ]
            })
        })
      
     
    }
    //自定义导出按钮
    tagClick = () => {
     
        console.log(this.state.startDate,this.state.endDate)

        if(this.state.startDate != '' && this.state.endDate != '') {

            if(this.state.text == '能源消耗统计表') {
                console.log('存在')
                request(`/hh-management-web/gnhj/energyconsumption/energyConsumptionListBySEExcelExport?startDate=${this.state.startDate}&endDate=${this.state.endDate}`).then(data => {
                    console.log(data)
                })
                window.location.href = `/hh-management-web/gnhj/energyconsumption/energyConsumptionListBySEExcelExport?startDate=${this.state.startDate}&endDate=${this.state.endDate}`
    
            }
    
            if(this.state.text == '土壤处理量统计表') {
                console.log('存在')
                request(`/hh-management-web/gnhj/soilTreatment/soilTreatmentListBySEExcelExport?startDate=${this.state.startDate}&endDate=${this.state.endDate}`).then(data => {
                    console.log(data)
                })
                window.location.href = `/hh-management-web/gnhj/soilTreatment/soilTreatmentListBySEExcelExport?startDate=${this.state.startDate}&endDate=${this.state.endDate}`
    
            }
    
            if(this.state.text == '土壤修复指标汇总表') {
                console.log('存在')
                request(`/hh-management-web/gnhj/soilremediation/soilRemediationExcelExport?startDate=${this.state.startDate}&endDate=${this.state.endDate}&isAll=${true}`).then(data => {
                    console.log(data)
                })
                window.location.href = `/hh-management-web/gnhj/soilremediation/soilRemediationExcelExport?startDate=${this.state.startDate}&endDate=${this.state.endDate}&isAll=${true}`
    
            }


        } else {
            message.warning('请选择时间')
        }

       


    }

    //重置按钮

 reset = () => {
     this.setState({
        endDate:"",
        rowCount:null,
        startDate:"",
        page:1,
        type1:"日报表",
        secondCity: cityData[provinceData[0]][0],
        tableLoading:true,
        RangePicker:null
     })
    
       //选择器中是自定义报表时发请求,成功后改变表格结构
       this.setState({
        reportType:0,

    })

    setTimeout(() => {
        let data = {
            page:1,
            reportName:this.state.secondCity,
            reportType:this.state.reportType,
            rows:10
        }
        querypaging({...data}).then(data => {
            console.log(data)
            let arr = data.data.ret.items
            arr.forEach(item => {
              item.takeTime = moment(item.takeTime).format("YYYY-MM-DD")
              if(item.reportType == 0) {
                  item.reportType="日报表"
              }
            });
            this.setState({
            tagVisible:false,
            tableLoading:false,
              data:data.data.ret.items,
              rowCount:data.data.ret.rowCount,
              columns: [
                {
                    title: "序号",
                    dataIndex: "num",
                    key: "num",
                    align: "center"
                },
                {
                    title: "报表类型",
                    dataIndex: "reportType",
                    key: "reportType",
                    align: "center"
                },
                {
                    title: "报表名称",
                    dataIndex: "reportChineseName",
                    key: "reportChineseName",
                    align: "center"
                },
                {
                    title: "日期",
                    key: "takeTime",
                    dataIndex: "takeTime",
                    align: "center"
                },
                {
                    title: "操作",
                    key: "useElectric",
                    dataIndex: "useElectric",
                    align: "center",
                    render:(text,record,index) => <span><Tag  color="blue" size="small" onClick={this.showModal.bind(this,record)}>查看</Tag ></span>
                }
            ]
            })
        })
    },300)

  

   
        
    }





    //查询按钮
    save = () => {
      this.setState({
        tableLoading:true,
        data:[],
        columns:[],
        repairWidth:null
      })
       //选择器中是自定义报表时发请求,成功后改变表格结构
        if(this.state.type == '自定义报表' && this.state.secondCity == '能源消耗统计表') {
            request(`/hh-management-web/gnhj/energyconsumption/queryEnergyConsumptionListBySE?startDate=${this.state.startDate}&endDate=${this.state.endDate}&page=${this.state.page}&rows=${this.state.rows}`).then(data => {
                console.log(data)
                if(data.data.rc == 0){
                    setTimeout(() => {
                        this.setState({
                            tableLoading:false,
                            tagVisible:true,
                            repairWidth:null,
                            columns: [
                                {
                                    title: "日期",
                                    dataIndex: "takeTime",
                                    key: "takeTime",
                                    align: "center"
                                },
                                {
                                    title: "燃气使用量/Nm³",
                                    dataIndex: "useNaturalGas",
                                    key: "useNaturalGas",
                                    align: "center"
                                },
                                {
                                    title: "电耗/KWh",
                                    dataIndex: "useElectric",
                                    key: "useElectric",
                                    align: "center"
                                },
                                {
                                    title: "水耗/t",
                                    key: "useWater",
                                    dataIndex: "useWater",
                                    align: "center"
                                }
                            ],
                            data:data.data.ret,
                            rowCount:data.data.rowCount
                        })
                    },500)
                   
                }
            })
        }

        if(this.state.type == '自定义报表' && this.state.secondCity == '土壤处理量统计表') {
            request(`/hh-management-web/gnhj/soilTreatment/querySoilTreatmentListBySE?startDate=${this.state.startDate}&endDate=${this.state.endDate}&page=${this.state.page}&rows=${this.state.rows}`).then(data => {
                console.log(data)
                if(data.data.rc == 0){
                    setTimeout(() => {
                        this.setState({
                            tableLoading:false,
                            tagVisible:true,
                            repairWidth:null,
                            columns: [
                                {
                                    title: "日期",
                                    dataIndex: "takeTime",
                                    key: "takeTime",
                                    align: "center"
                                },
                                {
                                    title: "土壤处理量/t",
                                    dataIndex: "throughput",
                                    key: "throughput",
                                    align: "center"
                                }
                            ],
                            data:data.data.ret,
                            rowCount:data.data.rowCount
                        })
                    },500)
                    
                }
            })
        }

        if(this.state.type == '自定义报表' && this.state.secondCity == '土壤修复指标汇总表') {


            let data = {
                endDate:this.state.endDate,
                startDate:this.state.startDate,
                page:1,
                rows:10,
                isAll:null

            }

            repairRefer({...data}).then(data => {
                console.log(data)
                if(data.data.rc == 0) {
                    data.data.ret.items.forEach(item => {
                        item.startTime = moment(item.startTime).format("YYYY-MM-DD")
                        if(item.endTime == null){
                               
                        } else {
                            item.endTime =moment(item.endTime).format("YYYY-MM-DD")
                        }
                      });
                }
              
                if(data.data.rc == 0){
                    setTimeout(() => {
                        this.setState({
                            tableLoading:false,
                            tagVisible:true,
                            repairWidth:2600,
                            columns: [
                                {
                                    title: "批次",
                                    dataIndex: "batches",
                                    key: "batches",
                                    align: "center"
                                },
                                {
                                    title: "开始时间",
                                    dataIndex: "startTime",
                                    key: "startTime",
                                    align: "center"
                                },
                                {
                                    title: "结束时间",
                                    dataIndex: "endTime",
                                    key: "endTime",
                                    align: "center"
                                },
                                {
                                    title: "处理时间",
                                    key: "processingTime",
                                    dataIndex: "processingTime",
                                    align: "center"
                                },
                                {
                                    title: "处理量/t",
                                    key: "throughput",
                                    dataIndex: "throughput",
                                    align: "center"
                                },
                                {
                                    title: "处理温度均值/℃",
                                    key: "processingTemperature",
                                    dataIndex: "processingTemperature",
                                    align: "center"
                                },
                                {
                                    title: "停留时间/min",
                                    key: "waitTime",
                                    dataIndex: "waitTime",
                                    align: "center"
                                },
                                {
                                    title: "六氯苯",
                                    align: "center",
                                    children: [
                                        {
                                            title: "处理前",
                                            dataIndex: "beforeProcessing1",
                                            key: "beforeProcessing1",
                                            align: "center"
                                        },
                                        {
                                            title: "处理后",
                                            dataIndex: "afterProcessing1",
                                            key: "afterProcessing1",
                                            align: "center"
                                        }
                                    ]
                                },
                                {
                                    title: "硝基苯",
                                    align: "center",
                                    children: [
                                        {
                                            title: "处理前",
                                            dataIndex: "beforeProcessing2",
                                            key: "beforeProcessing2",
                                            align: "center"
                                        },
                                        {
                                            title: "处理后",
                                            dataIndex: "afterProcessing2",
                                            key: "afterProcessing2",
                                            align: "center"
                                        }
                                    ]
                                },
                                {
                                    title: "苯并(a)芘",
                                    align: "center",
                                    children: [
                                        {
                                            title: "处理前",
                                            dataIndex: "beforeProcessing3",
                                            key: "beforeProcessing3",
                                            align: "center"
                                        },
                                        {
                                            title: "处理后",
                                            dataIndex: "afterProcessing3",
                                            key: "afterProcessing3",
                                            align: "center"
                                        }
                                    ]
                                },
                                {
                                    title: "苯并(a)蒽",
                                    align: "center",
                                    children: [
                                        {
                                            title: "处理前",
                                            dataIndex: "beforeProcessing4",
                                            key: "beforeProcessing4",
                                            align: "center"
                                        },
                                        {
                                            title: "处理后",
                                            dataIndex: "afterProcessing4",
                                            key: "afterProcessing4",
                                            align: "center"
                                        }
                                    ]
                                },
                                {
                                    title: "三氯乙烯",
                                    align: "center",
                                    children: [
                                        {
                                            title: "处理前",
                                            dataIndex: "beforeProcessing5",
                                            key: "beforeProcessing5",
                                            align: "center"
                                        },
                                        {
                                            title: "处理后",
                                            dataIndex: "afterProcessing5",
                                            key: "afterProcessing5",
                                            align: "center"
                                        }
                                    ]
                                },
                                {
                                    title: "1,4-二氯苯",
                                    align: "center",
                                    children: [
                                        {
                                            title: "处理前",
                                            dataIndex: "beforeProcessing6",
                                            key: "beforeProcessing6",
                                            align: "center"
                                        },
                                        {
                                            title: "处理后",
                                            dataIndex: "afterProcessing6",
                                            key: "afterProcessing6",
                                            align: "center"
                                        }
                                    ]
                                },
                                {
                                  title: "污染物质7",
                                  align: "center",
                                  children: [
                                      {
                                          title: "处理前",
                                          dataIndex: "beforeProcessing7",
                                          key: "beforeProcessing7",
                                          align: "center"
                                      },
                                      {
                                          title: "处理后",
                                          dataIndex: "afterProcessing7",
                                          key: "afterProcessing7",
                                          align: "center"
                                      }
                                  ]
                              },
                              {
                                title: "污染物质8",
                                align: "center",
                                children: [
                                    {
                                        title: "处理前",
                                        dataIndex: "beforeProcessing8",
                                        key: "beforeProcessing8",
                                        align: "center"
                                    },
                                    {
                                        title: "处理后",
                                        dataIndex: "afterProcessing8",
                                        key: "afterProcessing8",
                                        align: "center"
                                    }
                                ]
                            },
                                {
                                    title: "备注",
                                    key: "remarkText",
                                    dataIndex: "remarkText",
                                    align: "center"
                                }
                            ],
                            data:data.data.ret.items,
                            rowCount:data.data.ret.rowCount
                        })
                    },500)
                   
                }
            })
        }


        if(this.state.type == null || this.state.type == '日报表') {
           
            this.setState({
                reportType:0,
            })
            let data = {
                endDate:this.state.endDate,
                startDate:this.state.startDate,
                page:this.state.page,
                reportName:this.state.secondCity,
                reportType:this.state.reportType,
                rows:this.state.rows
            }
            //分页查询
            querypaging({...data}).then(data => {
                console.log(data)
                let arr = data.data.ret.items
                arr.forEach(item => {
                  item.takeTime = moment(item.takeTime).format("YYYY-MM-DD")
                  if(item.reportType == 0) {
                      item.reportType="日报表"
                  }
                });
                setTimeout(() => {
                    this.setState({
                        tableLoading:false,
                        tagVisible:false,
                      data:data.data.ret.items,
                      rowCount:data.data.ret.rowCount,
                      columns: [
                        {
                            title: "序号",
                            dataIndex: "num",
                            key: "num",
                            align: "center"
                        },
                        {
                            title: "报表类型",
                            dataIndex: "reportType",
                            key: "reportType",
                            align: "center"
                        },
                        {
                            title: "报表名称",
                            dataIndex: "reportChineseName",
                            key: "reportChineseName",
                            align: "center"
                        },
                        {
                            title: "日期",
                            key: "takeTime",
                            dataIndex: "takeTime",
                            align: "center"
                        },
                        {
                            title: "操作",
                            key: "useElectric",
                            dataIndex: "useElectric",
                            align: "center",
                            render:(text,record,index) => <span><Tag color="blue" size="small" onClick={this.showModal.bind(this,record)}>查看</Tag></span>
                        }
                    ]
                    })
                },500)
                
            })
        }
        
    }
    onShowSizeChange = (page,rows) => {
        console.log(page,rows)
        this.setState({
            tableLoading:true,
            page:page,
            rows:rows
        })
        setTimeout(() => {
            console.log(this.state.page,this.state.rows)
            this.save()
            this.setState({
                tableLoading:false
            })
        },100)
    }

    //分页器分页
    Pagination = (page,pageSize) => {
        console.log(page,pageSize)
        this.setState({
            page,
            rows:pageSize,
            tableLoading:true,
        })
        setTimeout(() => {
            this.save()
            this.setState({
                tableLoading:false
            })
        },100)
        
    }

    render() {
        const { cities } = this.state;
     
        return (
            <div>
                        <Examine
                            visible={this.state.visible}
                            onCancel={this.handleCancel}
                            record={this.state.record}
                            over={this.state.over}
                            text={this.state.text}
                        />
                        <Card
                            className={style.CardTop}
                        >
                            <div className={style.Top}>
                        报表类型:             
                        <Select
                            defaultValue={provinceData[0]}
                            style={{ width: 140,marginLeft:5 }}
                            onChange={this.handleProvinceChange}
                            className={style.ribiao}
                            value={this.state.type1}
                            >
                            {provinceData.map(province => <Option key={province}>{province}</Option>)}
                        </Select>
                        报表名称:
                        <Select
                            className={style.ribiao}
                            style={{ width: 240,marginLeft:5  }}
                            value={this.state.secondCity}
                            onChange={this.onSecondCityChange}
                            >
                            {cities.map(city => <Option key={city}>{city}</Option>)}
                        </Select>

                        查询时间: <RangePicker value={this.state.RangePicker}  onChange={this.RangePicker.bind(this.RangePicker)} placeholder={['开始时间','结束时间']}/>
                   
                        <Button type="primary" className={style.primary} onClick={this.save}>查询</Button>
                        <Button className={style.reset} onClick={this.reset}>重置</Button>
                            </div>

                        </Card>
                        <Card
                            className={style.CardBottom}
                            bodyStyle={{ paddingTop:'2vh',paddingBottom:"1vh"}}
                         >
                                         

                         <Tag color="blue" visible={this.state.tagVisible} onClick={this.tagClick}>导出</Tag>
                        <div className={style.myTable}>
                        <Table
                            loading={this.state.tableLoading}
                            size="small"
                            columns={this.state.columns}
                            dataSource={publicService.transformArrayData(this.state.data,true,true)}
                            style={{width:this.state.repairWidth}}
                            bordered={true}
                            pagination={false}
                            className={style.Table}
                        />
                        </div>
                        <Pagination
                            showQuickJumper
                            showSizeChanger
                            onShowSizeChange={this.onShowSizeChange}
                            defaultCurrent={1}
                            total={this.state.rowCount}
                            onChange={this.Pagination}
                            className={style.Pagination}
                            size="small"
                            current={this.state.page}
                        />
                    
                        </Card>
                      

             
                  

            </div>

        )
    }
}

export default connect()(Inquire)